const app=getApp()
const db=wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  id:"",
  openid:"",
  timeIndex:0,
  time:[5,10,15,20],
  inputid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  console.log(options)
  this.setData({
    id:options.id
  })
  },
  inputOpenid: function(e){
    this.setData({
      inputid:e.detail.value
    })
  },
  chooseTime: function(e){
    console.log(e.detail)
    this.setData({
      timeIndex: e.detail.value
    })
  },
  readyForSubmit: function(){
    
    
    
    if(this.data.inputid==""){
      wx.showToast({
        title: '未填写ID',
        icon:'none',
        duration:2000
      })
      return;
    }
    

    else{
      wx.showLoading({
        title:'上传中',
      })
      
     
      
      db.collection('User').where({
         phoneNumber:this.data.inputid
      }).get({
        success:res=>{
          console.log(res.data)
          db.collection('CourseSales').add({
            data:{
              _openid:res.data[0].openid,
              courseId:this.data.id,
              remainingTimes:this.data.time[this.data.timeIndex],
              times:this.data.time[this.data.timeIndex],
              studentID:res.data[0].openid,
              sign:[3],
              status:0
            },
            success:function(res){
              wx.hideLoading()
              wx.showToast({
                title: '提交成功',
              })
              wx.navigateBack({
                delta: 1,
              })
            },
            fail:function(res){
              wx.hideLoading()
              wx.showToast({
                title: '提交失败',
                icon:"none"
              })
            }
          })
        }
      })
     
     
    
      
    }
       
    
    
  },
})